package com.jsoft.jsofted.sys.service.impl;

import com.jsoft.jsofted.sys.entity.UserEntity;
import com.jsoft.jsofted.sys.mapper.UserMapper;
import com.jsoft.jsofted.framework.mybatis.CocQueryWrapper;
import com.jsoft.jsofted.framework.service.impl.CocServiceImpl;
import com.jsoft.jsofted.sys.service.GroupService;
import com.jsoft.jsofted.sys.service.UserService;
import com.jsoft.jsofted.framework.utils.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

@Service("userService")
public class UserServiceImpl extends CocServiceImpl<UserMapper, UserEntity> implements UserService {

    @Autowired
    private GroupService groupService;

    @Override
    public UserEntity getByUsername(String username) {
        return this.baseMapper.getByUsername(username);
    }

    @Override
    public UserEntity getForLogin(String username, String password) {
        return this.baseMapper.getForLogin(username, password);
    }

    protected CocQueryWrapper parseQueryWrapper(Map queryParams) {
        if (queryParams == null) {
            queryParams = new LinkedHashMap();
        }

        CocQueryWrapper queryWrapper = new CocQueryWrapper();

        // 解析【查询参数】中的【排序参数】
        this.parseSortParams(queryWrapper, queryParams);

        // 解析【条件参数】中的【ID参数】
        this._parseIdParams(queryWrapper, queryParams);

        // 解析【条件参数】中的【机构参数】
        this._parseOrgParams(queryWrapper, queryParams);

        // 解析【条件参数】中的【职位参数】
        this._parsePostParams(queryWrapper, queryParams);

        // 解析【条件参数】中的【性别参数】
        this._parseSexParams(queryWrapper, queryParams);

        // 解析【条件参数】中的【关键字参数】
        this._parseKeywordsParams(queryWrapper, queryParams);

        return queryWrapper;
    }

    /**
     * 解析【条件参数】中的【ID参数】
     */
    private void _parseIdParams(CocQueryWrapper<UserEntity> queryWrapper, Map queryParams) {
        List<String> idList = (List<String>) queryParams.get("idList");
        if (idList != null && idList.size() > 0) {
            queryWrapper.in("id", idList);
        }
    }

    /**
     * 解析【条件参数】中的【关键字参数】
     */
    private void _parseKeywordsParams(CocQueryWrapper<UserEntity> queryWrapper, Map queryParams) {
        String keywords = (String) queryParams.get("keywords");
        if (keywords != null && keywords.trim().length() > 0) {
            Consumer<CocQueryWrapper<UserEntity>> consumer = qw -> {
                qw.like("name", keywords);
                qw.or();
                qw.like("tel", keywords);
                qw.or();
                qw.like("username", keywords);
            };
            queryWrapper.and(consumer);
        }
    }

    /**
     * 解析【条件参数】中的【性别参数】
     */
    private void _parseSexParams(CocQueryWrapper<UserEntity> queryWrapper, Map queryParams) {
        String sex = (String) queryParams.get("sex");
        if (sex != null && sex.trim().length() > 0) {
            queryWrapper.eq("sex", sex);
        }
    }

    /**
     * 解析【条件参数】中的【职位参数】
     */
    private void _parsePostParams(CocQueryWrapper queryWrapper, Map queryParams) {
        List<String> postIdList = new ArrayList<>();
        List<String> _postIdList = (List<String>) queryParams.get("postIdList");
        if (_postIdList != null && _postIdList.size() > 0) {
            postIdList.addAll(_postIdList);
        }
        String postId = (String) queryParams.get("postId");
        if (StringUtils.hasText(postId) && !postIdList.contains(postId)) {
            postIdList.add(postId);
        }

        if (postIdList.size() > 0) {
            String postIdString = ObjectUtils.join(postIdList, "|");
            Consumer<CocQueryWrapper<UserEntity>> consumer = qw -> {
                qw.regexp("post_id", postIdString);
                qw.or();
                qw.subSelect("id", "IN", "(SELECT DISTINCT(user_id) FROM sys_user_post WHERE post_id REGEXP {0})", postIdString);
            };
            queryWrapper.and(consumer);
        }
    }

    /**
     * 解析【条件参数】中的【机构参数】
     */
    private void _parseOrgParams(CocQueryWrapper queryWrapper, Map queryParams) {
        final String orgId = (String) queryParams.get("orgId");
        //
        final List<String> orgIdList = new ArrayList();
        List<String> _orgIdList = (List<String>) queryParams.get("orgIdList");
        if (_orgIdList != null && _orgIdList.size() > 0) {
            orgIdList.addAll(_orgIdList);
        }
        if (StringUtils.hasText(orgId) && !orgIdList.contains(orgId)) {
            orgIdList.add(orgId);
        }
        if (orgIdList.size() > 0) {
            Consumer<CocQueryWrapper<UserEntity>> consumer = qw -> {
                if (StringUtils.hasText(orgId)) {
                    qw.subSelect("org_id", "REGEXP", "getOrgTreeIds({0})", orgId);
                    qw.or();
                    qw.subSelect("id", "IN", "(SELECT DISTINCT(user_id) FROM sys_user_post WHERE org_id REGEXP getOrgTreeIds({0}))", orgId);
                } else {
                    String orgIdString = ObjectUtils.join(orgIdList, "|");
                    qw.regexp("org_id", orgIdString);
                    qw.or();
                    qw.subSelect("id", "IN", "(SELECT DISTINCT(user_id) FROM sys_user_post WHERE org_id REGEXP {0})", orgIdString);
                }
            };
            queryWrapper.and(consumer);
        }
    }

}
